package com.huabo.fxgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huabo.fxgl.entity.Organization;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huabo.fxgl.entity.Find;
import com.huabo.fxgl.entity.RiskAssplan;
import com.huabo.fxgl.entity.RiskAssplanRisk;
import com.huabo.fxgl.entity.YyCompany;
import com.huabo.fxgl.mapper.RiskAssplanMapper;
import com.huabo.fxgl.service.IRiskAssplanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiYe
 * @since 2022-08-01
 */
@Service
public class RiskAssplanServiceImpl extends ServiceImpl<RiskAssplanMapper, RiskAssplan> implements IRiskAssplanService {
    @Autowired
    private RiskAssplanMapper riskAssplanMapper;
    @Autowired
    private RiskAssplanRiskServiceImpl assplanRiskService;
    @Override
    public IPage<RiskAssplan> findAll(IPage page, RiskAssplan riskAssplan, String orgid){
        QueryWrapper<RiskAssplan> queryWrapper=new QueryWrapper();
        if (StringUtils.isNotBlank(orgid))
            queryWrapper.eq("unit", orgid);
        if (riskAssplan!=null && riskAssplan.getPlancode()!=null && riskAssplan.getPlancode().length()>0){
            queryWrapper.like("plancode",riskAssplan.getPlancode());
        }
        if (riskAssplan!=null && riskAssplan.getPlanName()!=null && riskAssplan.getPlanName().length()>0){
            queryWrapper.like("planname",riskAssplan.getPlanName());
        }
        if (riskAssplan!=null && riskAssplan.getPlanType()!=null && riskAssplan.getPlanType().length()>0){
            queryWrapper.eq("plantype",riskAssplan.getPlanType());
        }
        if (riskAssplan!=null && riskAssplan.getPlanStatus()!=null && riskAssplan.getPlanStatus().length()>0){
            queryWrapper.eq("planstatus",riskAssplan.getPlanStatus());
        }
        if (riskAssplan!=null) {
            if (riskAssplan.getStartDate()!=null) {
                queryWrapper.gt("startdate", riskAssplan.getStartDate());
            }
            if (riskAssplan.getEndDate()!=null) {
                queryWrapper.lt("enddate", riskAssplan.getEndDate());
            }
        }
        queryWrapper.orderByDesc("plancode");
        return page(page,queryWrapper);
    }

    /**
     * 查找评估结果页面的分页详情信息
     * @param page 分页
     * @param find 一个工具类
     * @Date 2022/8/10
     * @author wanghongtuo
     * @version 1.0.1
     * @return
     */
    @Override
    public IPage<RiskAssplan> findRiskAssPlanPageDetails(IPage page, Find find) {
        QueryWrapper<RiskAssplan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("planStatus", '3');
        if(StringUtils.isNotEmpty(find.getCode()))
            queryWrapper.like("plancode",find.getCode());

        if(StringUtils.isNotEmpty(find.getName()))
            queryWrapper.like("planName",find.getName());

        if(find != null && find.getId()!= null)
            queryWrapper.eq("unit",find.getId());
        queryWrapper.orderByDesc("recordDate");

        IPage pageBean = page(page, queryWrapper);
        List<RiskAssplan> recordList = pageBean.getRecords();
        for (RiskAssplan  riskAssplan : recordList) {
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("ASSPLANID", riskAssplan.getAssplanid());
            List<RiskAssplanRisk> assPlanRisks = assplanRiskService.list(queryWrapper1);
            for (RiskAssplanRisk riskAssPlanRisk : assPlanRisks) {
                switch (Integer.parseInt(riskAssPlanRisk.getRisklevel())) {
                    case 1:
                        riskAssplan.setCount1(riskAssplan.getCount1() + 1);
                        break;
                    case 2:
                        riskAssplan.setCount2(riskAssplan.getCount2() + 1);
                        break;
                    case 3:
                        riskAssplan.setCount3(riskAssplan.getCount3() + 1);
                        break;
                    case 4:
                        riskAssplan.setCount4(riskAssplan.getCount4() + 1);
                        break;
                    case 5:
                        riskAssplan.setCount5(riskAssplan.getCount5() + 1);
                        break;
                    default:
                        break;
                }
            }
            riskAssplan.setCount(riskAssplan.getCount1() + riskAssplan.getCount2() + riskAssplan.getCount3()
                    + riskAssplan.getCount4() + riskAssplan.getCount5());
        }
        return pageBean;
    }
}
